package mspbots.data.tl.prod.jira.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultMessageModel;
import mspbots.data.tl.prod.jira.mapper.JiraSprintMapper;
import mspbots.data.tl.prod.jira.model.JiraSprint;
import mspbots.data.tl.prod.jira.service.JiraSprintService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@DS("master")
public class JiraSprintServiceImpl extends AbstractBaseServiceImpl<JiraSprintMapper, JiraSprint>
        implements JiraSprintService {

    @Override
    public void sync(ResultMessageModel model) {
        if (ObjectUtils.isEmpty(model.getEntity()) || !ResultMessageModel.MessageModelAction.ADD.equals(model.getAction())) {
            log.error("jira.sprint.tl entry is null,or Action No Add");
            return;
        }
        Iterator<JsonNode> suggestions = model.getEntity().get("suggestions").elements();
        Iterator<JsonNode> allMatches = model.getEntity().get("allMatches").elements();
        while (suggestions.hasNext()){
            processData(model, suggestions,"suggestions");
        }
        while (allMatches.hasNext()){
            processData(model, allMatches,"allMatches");
        }
    }

    private void processData(ResultMessageModel model, Iterator<JsonNode> appointments,String type) {
        JsonNode node = appointments.next();
        JiraSprint data = JiraSprint.builder()
                .sprintId(node.get("id").asLong())
                .name(node.get("name").asText())
                .stateKey(node.get("stateKey").asText())
                .boardName(node.get("boardName").asText())
                .date(processDate(node.get("date").asText()))
                .type(type)
                .syncTime(LocalDateTime.now())
                .build();

        data.setTenantCode(model.getTenantId());

        JiraSprint dbData = super.baseMapper.selectOne(Wrappers.<JiraSprint>lambdaQuery()
                .eq(JiraSprint::getTenantCode, data.getTenantCode())
                .eq(JiraSprint::getSprintId, data.getSprintId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }

    private LocalDateTime processDate(String date){
        try {
            return !StringUtils.isEmpty(date) ?
                    LocalDateTime.parse(date.replace("Z","0"), DateTimeFormatter.ISO_DATE_TIME) : null;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

}
